package com.yogee.topseller.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.github.chrisbanes.photoview.PhotoView;
import com.yogee.topseller.R;
import com.yogee.topseller.utils.GlideUtils;

import java.util.List;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/7/17.
 */

public class PhotoPageAdapter extends PagerAdapter {
    private List<String> img;
    private Context context;

    public PhotoPageAdapter(Context context) {
        this.context = context;
    }

    private OnPageItemClickListener onPageItemClickListener;

    public void addData(List<String> img) {
        this.img = img;
        notifyDataSetChanged();
    }

    public void setOnPageItemClickListener(OnPageItemClickListener onPageItemClickListener) {
        this.onPageItemClickListener = onPageItemClickListener;
    }

    @Override
    public int getCount() {
        return img == null ? 0 : img.size();
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        PhotoView photoView = new PhotoView(container.getContext());
        GlideUtils.defultImage(context,img.get(position),photoView);

        // Now just add PhotoView to ViewPager and return it
        container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onPageItemClickListener.onPageItemClick();
            }
        });

        return photoView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    public interface OnPageItemClickListener {
        void onPageItemClick();
    }
}
